﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace MiniCrawler.UrlFilter
{
        public class ExpressionObject
        {
            public VerbalExpressions VerbalExpression { get; set; }

            // Constructions
            public ExpressionObject()
            {

            }
            public ExpressionObject(string xmlTemplate)
            {
                var t = ConvertFromXml(xmlTemplate);
                this.VerbalExpression = t.VerbalExpression;
            }

            // Methods
            public ExpressionObject ConvertFromXml(string xmlExpression)
            {

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlExpression);


                XmlElement rootElement = doc.DocumentElement;
                if (rootElement == null) return null;
                
                XmlElement expressionParts = rootElement["ExpressionParts"];
                VerbalExpressions verbal = new VerbalExpressions();
                if (expressionParts != null)
                {

                    foreach (XmlNode expressionPart in expressionParts.ChildNodes)
                    {
                        string command = expressionPart.ChildNodes[0].InnerText;
                        string value = expressionPart.ChildNodes[1].InnerText;
                        verbal = GetCommand(verbal, command, value);
                    }
                }

                ExpressionObject result = new ExpressionObject
                {
                    VerbalExpression = verbal
                };
                return result;
            }
            public VerbalExpressions GetCommand(VerbalExpressions exp, string expression, string value)
            {
                var resultExpression = exp;
                if (expression.Equals("StartOfLine"))
                {
                    if (value.Equals("true"))
                        resultExpression = resultExpression.StartOfLine(true);
                    else if (value.Equals("false"))
                        resultExpression = resultExpression.StartOfLine(false);
                    else
                    {
                        resultExpression = resultExpression.StartOfLine();
                    }
                }

                if (expression.Equals("AnythingBut"))
                {
                    resultExpression = resultExpression.AnythingBut(value);
                }
                if (expression.Equals("Then"))
                {
                    resultExpression = resultExpression.Then(value);
                }
                if (expression.Equals("IsSequenseOfNumber"))
                {
                    resultExpression = resultExpression.IsSequenseOfNumber();
                }
                if (expression.Equals("EndOfLine"))
                {
                    resultExpression = resultExpression.EndOfLine(true);
                }
                return resultExpression;

            }

            public bool IsMatch(string stringToTest)
            {

                return VerbalExpression.IsMatch(stringToTest);
            }
            
        }
}
